/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.capiq.server.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.excilys.capiq.shared.QuestionDTO;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Question {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long			id;

	@Persistent
	private String			author;

	@Persistent
	private String			question;

	@Persistent
	private List<String>	answers;

	@Persistent
	private int				goodAnswerIndex;

	@Persistent
	private String			explanation;

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public List<String> getAnswers() {
		return answers;
	}

	public void setAnswers(List<String> answers) {
		this.answers = answers;
	}

	public int getGoodAnswerIndex() {
		return goodAnswerIndex;
	}

	public void setGoodAnswerIndex(int goodAnswerIndex) {
		this.goodAnswerIndex = goodAnswerIndex;
	}

	public Long getId() {
		return id;
	}

	public QuestionDTO toQuestionDTO() {
		QuestionDTO questionDTO = new QuestionDTO();
		questionDTO.setId(id);
		questionDTO.setAuthor(author);

		/*
		 * TODO this is necessary since we should not serialize the datanucleus
		 * arraylist. Maybe we could come up with something else ?
		 */
		List<String> answersDTO = new ArrayList<String>();
		for (String answer : answers) {
			answersDTO.add(answer);
		}

		Collections.shuffle(answersDTO);

		questionDTO.setAnswers(answersDTO);
		questionDTO.setQuestion(question);

		return questionDTO;
	}

	public String getExplanation() {
		return explanation;
	}

	public void setExplanation(String explanation) {
		this.explanation = explanation;
	}

}
